package com.ruoyi.example.swagger;

import com.ruoyi.common.core.controller.custom.CustomController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.web.core.config.ApiResponseMap;
import com.ruoyi.web.core.config.ApiResponseProperty;
import com.ruoyi.web.core.config.SwaggerMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController("SwaggerUser/")
@Api(description = "swagger测试_用户接口")
public class SwaggerUserController extends CustomController {

    /**
     *   关于接口返回参数使用@ApiResponseMap 注解的使用说明：
     *   1、返回标准的列表数据集  [{}]
     *   1、返回标准的对象  {}
     *   2、返回规范的响应接口（数组）：｛code:0,msg:'',data:[{}]}
     *   3、返回规范的响应接口（对象）：｛code:0,msg:'',data:{}}
     *
     *   一、基本描述
     *
     *   @ApiResponseMap 该注解主要用于解决, 当接口返回值为Map时, swagger无法对数据的字段列，生成相对应的文档描述。
     *
     *   二、两种编写方式
     *
     *   @ApiResponseMap 该注解有两种方式编写字段的描述：(一)通过@ApiResponseProperty 注解在方法上进行编写。该方式优点是字段描述直接在接口上方，操作方便，
     *   有较好的直观性。缺点：如果字段描述较多，过多的注解描述影响美观；(二)在SwaggerMap 的 init方法中，单独定义描述。优点，字段较多时描述单独定义在SwaggerMap类中，
     *   则接口上方较为简洁。缺点，操作稍微复杂一些；注意：两种方式同时使用时，优先使用 @ApiResponseProperty 注解方式。
     *
     *   三、命名规范
     *
     *   (一)@ApiResponseMap注解的modleName 必须全局唯一。
     *   1、 使用@ApiResponseProperty方式：modleName = 完全限定名+方法名
     *   2、 使用@ApiResponseMap方式：在方法中定义常量值：public final static final String  属性名 = 前缀+属性名；
     */

    //全局唯一完全限定名。注意："." 修改为 "_"
    private final String modleName = "com_ruoyi_web_controller_swaggerDemo_SwaggerUserController";


    /**
     * 使用注解方式描述列字段
     */
    @GetMapping("/getUserList")
    @ApiOperation(value = "获取用户信息列表（分页）", notes = "根据用户ID进行筛选", tags = "CRUD")
    @ApiResponseMap(modleName = modleName + "getUserList", properties = {
            @ApiResponseProperty(name = "id", description = "用户编号", type = "string"),
            @ApiResponseProperty(name = "name", description = "用户名", type = "string"),
            @ApiResponseProperty(name = "sex", description = "性别", type = "string")
    })
    public TableDataInfo getUserList(@ApiParam("部门编码") @RequestParam("deptId") String deptId) {
        System.out.println("请求：getUserList 接口, id" + deptId);

        /*测试数据*/
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> map1 = new HashMap<>();
        map1.put("id", "001");
        map1.put("name", "刘备");
        map1.put("sex", "男");
        list.add(map1);

        Map<String, Object> map2 = new HashMap<>();
        map2.put("id", "002");
        map2.put("name", "曹操");
        map2.put("sex", "男");
        list.add(map2);

        Map<String, Object> map3 = new HashMap<>();
        map3.put("id", "003");
        map3.put("name","孙权");
        map3.put("sex","男");
        list.add(map3);

        return getDataTable(list);
    }

    /**
     * 在SwaggerMap中定义列描述
     */
    @GetMapping("/UserInfo/:id")
    @ApiOperation(value = "获取单个用户信息")
    @ApiResponseMap(modleName = SwaggerMap.getUserInfoById)
    public AjaxResult getUserInfoById(@ApiParam("用户ID") @RequestParam("id") String id) {
        /*测试数据*/
        Map<String,Map<String,Object>> prentMap = new HashMap();

        Map<String,Object> map1 = new HashMap<>();
        map1.put("id","001");
        map1.put("name","唐僧");
        map1.put("sex","男");
        map1.put("type","僧人");
        prentMap.put("001",map1);

        Map<String,Object> map2 = new HashMap<>();
        map2.put("id","002");
        map2.put("name","孙悟空");
        map2.put("sex","男");
        map2.put("type","猴子");
        prentMap.put("002",map2);

        Map<String,Object> map3 = new HashMap<>();
        map3.put("id","003");
        map3.put("name","猪八戒");
        map3.put("sex","男");
        map3.put("type","猪");
        prentMap.put("003",map3);

        Map<String,Object> map4 = new HashMap<>();
        map4.put("id","004");
        map4.put("name","沙僧");
        map4.put("sex","男");
        map4.put("type","沙弥");
        prentMap.put("004",map4);

        Map<String,Object> map5 = new HashMap<>();
        map5.put("id","005");
        map5.put("name","蜘蛛精");
        map5.put("sex","女");
        map5.put("type","妖怪");
        prentMap.put("005",map5);
        return AjaxResult.success(prentMap.get(id));
    }

    @GetMapping("/all")
    @ApiOperation(value = "获取用户列表（无分页）")
//    @ApiResponseMap(modleName = SwaggerMap.all)
    @ApiResponseMap(modleName = modleName + "getUserList", properties = {
            @ApiResponseProperty(name = "id", description = "用户编号", type = "string"),
            @ApiResponseProperty(name = "name", description = "用户名", type = "string"),
            @ApiResponseProperty(name = "sex", description = "性别", type = "string")
    })
    public List<Map<String,Object>> all() {
        /*测试数据*/
        List<Map<String,Object>> list = new ArrayList<>();
        Map<String,Object> map1 = new HashMap<>();
        map1.put("id","001");
        map1.put("name","吕布");
        map1.put("age","18");
        Map<String,Object> map2 = new HashMap<>();
        map2.put("id","002");
        map2.put("name","貂蝉");
        map2.put("age","19");
        return list;
    }

    /**
     * 如果增删改，没有返回data的数据，只需要生成 code、msg、data 等参数的描述，按如下方式就可以
     */
    //更新
    @PutMapping("/updateUserInfo")
    @ApiOperation(value = "更新用户信息")
    @ApiResponseMap(modleName = "update")
    public AjaxResult updateUser() {
        return AjaxResult.success();
    }
    //添加
    @PostMapping("/updateUserInfo")
    @ApiOperation(value = "添加用户信息")
    @ApiResponseMap(modleName = "insert")
    public AjaxResult insertUser() {
        return AjaxResult.success();
    }
    //删除
    @DeleteMapping("/updateUserInfo")
    @ApiOperation(value = "删除用户信息")
    @ApiResponseMap(modleName = "delete")
    public AjaxResult deleteUser() {
        return AjaxResult.success();
    }

}

